package com.laxcen.toydemo;

import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.apkfuns.logutils.LogUtils;
import com.laxcen.controls.HomeMenuButtonControl;
import com.laxcen.intelligenttoy.BLE.ETag;
import com.laxcen.lib.GameFailedDialog;
import com.laxcen.lib.GameSuccessDialog;

import java.util.ArrayList;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;

import util.BackgroundMusic;
import util.DefaultHandler;
import util.DelayHandler;
import util.RandomUtil;
import util.SoundClass;
import util.TagItem;
import util.TagResult;

public class GameStep4Activity extends BaseActivity implements View.OnClickListener {
    GameState mGameState = GameState.instance(this);
    HomeMenuButtonControl btn_muban, btn_home;
    HomeMenuButtonControl btn_option1, btn_option4;
    HomeMenuButtonControl btn_daxiao, btn_daku;
    HomeMenuButtonControl img_xiaoxiong_eat_apple;
    ImageView imgBg;
    RelativeLayout layoutBg1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_step4);
        mGameState.start();

        btn_home = findViewById(R.id.btn_home);
        btn_home.setOnClickListener(this);

        btn_muban = findViewById(R.id.btn_muban);
        btn_muban.setOnClickListener(this);

        btn_option1 = findViewById(R.id.btn_option1);
        btn_option1.setOnClickListener(this);
        btn_option4 = findViewById(R.id.btn_option4);
        btn_option4.setOnClickListener(this);

        btn_daxiao = findViewById(R.id.btn_daxiao);
        btn_daxiao.setOnClickListener(this);
        btn_daku = findViewById(R.id.btn_daku);
        btn_daku.setOnClickListener(this);


        img_xiaoxiong_eat_apple = findViewById(R.id.img_xiaoxiong_eat_apple);

        imgBg = findViewById(R.id.imgBg);

        layoutBg1 = findViewById(R.id.layoutBg1);
    }

    private void startRepeatPromptGbSound() {
        new DefaultHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mGameState.getStep() == GameState.EGameStep.GameStep4) {
                    BackgroundMusic.instance(GameStep4Activity.this).play(R.raw.audio_game_step4_repeat_prompt, false);
                }
            }
        }, 2000);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_apple1 || v.getId() == R.id.btn_apple2) {
            HomeMenuButtonControl btn_apple = (HomeMenuButtonControl) v;
//            btn_apple.shake();
        } else if (v.getId() == R.id.btn_home) {
            goback();
        } else if (v.getId() == R.id.btn_option1) {
            onOption1Active();
        } else if (v.getId() == R.id.btn_option4) {
            onOption4Active();
        }
    }

    private void goback() {
        isWaitTag = false;
        BackgroundMusic.instance(this).stop();
        startActivityAndFinish(GameStepSelectActivity.class);
    }

    private void onOption4Active() {

        onRightOptionActive();
    }

    private void onOption1Active() {

        DelayHandler.newHandler().delay(new Runnable() {
            @Override
            public void run() {
                img_xiaoxiong_eat_apple.show();
                btn_daku.show();
                btn_daku.bigSplash();
                new SoundClass(GameStep4Activity.this).playSound(R.raw.audio_daku);
            }
        }, 1000).delay(new Runnable() {
            @Override
            public void run() {
                new SoundClass(GameStep4Activity.this).playSound(R.raw.audio_game_step4_error_prompt);
            }
        }, 3000).delay(new Runnable() {
            @Override
            public void run() {
                showFalied();
            }
        }, 5000);
    }

    private void onRightOptionActive() {
        layoutBg1.setVisibility(View.VISIBLE);
        DelayHandler.newHandler().delay(new Runnable() {
            @Override
            public void run() {

                img_xiaoxiong_eat_apple.show();
                btn_daxiao.show();
                btn_daxiao.bigSplash();
                new SoundClass(GameStep4Activity.this).playSound(R.raw.audio_daxiao);
            }
        }, 1000).delay(new Runnable() {
            @Override
            public void run() {
                new SoundClass(GameStep4Activity.this).playSound(R.raw.audio_game_step4_success_prompt);

                imgBg.setImageDrawable(ContextCompat.getDrawable(GameStep4Activity.this, R.mipmap.game4_bg2));
            }
        }, 6000).delay(new Runnable() {
            @Override
            public void run() {
                layoutBg1.setVisibility(View.GONE);
                gotoGoodByeForDuck();
            }
        }, 6000).delay(new Runnable() {
            @Override
            public void run() {
                gotoXiaoxiongHome();
            }
        }, 6000).delay(new Runnable() {
            @Override
            public void run() {
                showSuccess();
            }
        }, 9000);
    }

    private void gotoGoodByeForDuck() {
        final Animation animation = AnimationUtils.loadAnimation(GameStep4Activity.this, R.anim.game1_bg_small_to_big_anim);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
//                imgBg.setVisibility(View.GONE);
                imgBg.setImageDrawable(ContextCompat.getDrawable(GameStep4Activity.this, R.mipmap.game4_boodbye_for_duck_bg));
                new SoundClass(GameStep4Activity.this).playSound(R.raw.audio_game_step4_say_goodbye);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        imgBg.startAnimation(animation);
    }

    private void gotoXiaoxiongHome() {
        final Animation animation = AnimationUtils.loadAnimation(GameStep4Activity.this, R.anim.game1_bg_small_to_big_anim);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
//                imgBg.setVisibility(View.GONE);
                imgBg.setImageDrawable(ContextCompat.getDrawable(GameStep4Activity.this, R.mipmap.game4_goto_home_bg));
                new SoundClass(GameStep4Activity.this).playSound(R.raw.audio_game_step4_dad_very_happy);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        imgBg.startAnimation(animation);
    }

    private void showSuccess() {
        GameSuccessDialog successDialog = GameSuccessDialog.getInstance(this);
        successDialog.show("", RandomUtil.generateOneNumber(0, 5));
        successDialog.setGobackClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goback();
            }
        });
        successDialog.setGoNextClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityAndFinish(GameStep5Activity.class);
            }
        });
    }

    private void showFalied() {
        GameFailedDialog failedDialog = GameFailedDialog.getInstance(this);
        failedDialog.show();
        failedDialog.setGobackClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goback();
            }
        });
        failedDialog.setGoNextClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                startActivity(GameStep4Activity.class);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        BackgroundMusic.instance(this).stop();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mGameState.setStep(GameState.EGameStep.GameStep4);
        startRepeatPromptGbSound();
        restartTagCheck();
    }

    private boolean isWaitTag = true;

    private void restartTagCheck() {
        initQueue();
        DelayHandler.newHandler().delay(new Runnable() {
            @Override
            public void run() {
                isWaitTag = true;
                startTagCheck();
            }
        }, 1000);
    }

    private void initQueue() {
        tagItemQueue.clear();
        tagItemQueue.add(new TagItem(ETag._1, false, R.raw.game_step4_tag2));
        tagItemQueue.add(new TagItem(ETag._ADD, false, R.raw.game_step4_tag3));
        tagItemQueue.add(new TagItem(ETag._3, false, R.raw.game_step4_tag4));
        tagItemQueue.add(new TagItem(ETag._EQUALS, false, R.raw.game_step4_tag5));
        tagItemQueue.add(new TagItem(ETag._4, false, R.raw.game_step4_tagok));
    }

    /* 7,8,9 = 1,2,3 */
    private synchronized void startTagCheck() {
        if (!isWaitTag) return;
        if (checkOption(currentTags, ETag._GOBACK)) {
            isWaitTag = false;
            goback();
            return;
        }
        TagResult tagResult = checkHasString(currentTags);
        if (tagResult.getHasString()) {
            String tagString = tagResult.getTagString();
            TagItem tagItem = tagItemQueue.peek();
            LogUtils.i("当前托盘------->>" + tagString + "          已经过-------->>" + usedTagList);
            //棋盘以 当前 游标为 结尾
            String tempUsedTagList = usedTagList + tagItem.getTag();
            LogUtils.i("需要的Tag------------>>" + tempUsedTagList);
            if (tagString.endsWith(tagItem.getTag()) && tagString.equals(tempUsedTagList)) {
                tagItemQueue.poll();
                usedTagList += tagItem.getTag();
                LogUtils.i("Ok------------>>" + usedTagList);
                new SoundClass(GameStep4Activity.this).playSound(tagItem.getAudio());
                if (tagItemQueue.isEmpty()) {
                    isWaitTag = false;
                    LogUtils.i("END------------>>" + usedTagList);
                    DelayHandler.newHandler().delay(new Runnable() {
                        @Override
                        public void run() {
                            showSuccess();
                        }
                    }, 3000);
                }
            } else {
                LogUtils.i("没有命中------------>>");
            }
        }
//        else if (checkMatchStringTag(currentTags, ETag._1 + ETag._ADD + ETag._3 + ETag._EQUALS + ETag._4)) {
//            isWaitTag = false;
//            onOption4Active();
//        }

        if (isWaitTag) {
            DelayHandler.newHandler().delay(new Runnable() {
                @Override
                public void run() {
                    startTagCheck();
                }
            }, LoopDetectionMillis);
        }
    }

    private Queue<TagItem> tagItemQueue = new ArrayBlockingQueue<TagItem>(5);
    private String usedTagList = "";


}
